import 'dart:convert';

import 'package:client_flutter_android/bean/cabinet.dart';
import 'package:client_flutter_android/vars.dart';


// import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class Box {
  static Future<List<Cabinet>> getBoxs() async {
    var request = http.Request('GET', Uri.parse('${Vars.boxUrl}/locks'));
    http.StreamedResponse response = await request.send();
    if (response.statusCode == 200) {
      debugPrint('@');
      String result = await response.stream.bytesToString();
      debugPrint('*');
      List<dynamic> boxs = json.decode(result);
      List<Cabinet> data = [];
      debugPrint('**');
      for (var i = 0; i < boxs.length; i++) {
        Map map = boxs[i] as Map<String, dynamic>;
        Cabinet cabinet = Cabinet(
            id: map['id'],
            BlId: map['BlId'],
            DeviceId: map['DeviceId'],
            mainboardno: map['mainboardno'],
            lockno: map['lockno'],
            czh: map['czh'],
            occupied: map['occupied'],
            is400AF: map['is400AF'],
        );
        data.add(cabinet);
      }

      return data;
    } else {
      return [];
    }
  }

}
